<?php

namespace app\common\model\logic;

class ResourceModel
{
    public function __construct()
    {
        $this->curModel = model('dao.resource');
    }

    //图片上传
    public function uploadImg()
    {
        $file = request()->file('img_file');
        $fileDir = $this->getUploadDir();
        $fileName = $this->getFileName($file);
        $info = $file->validate(['ext' => 'jpeg,jpg,png,gif', 'file_size' => '10*1024*1024'])->move('.'.$fileDir, $fileName);
        if ($info) {
            $data['thumbnail_url'] = $this->setThumbnailImg($fileDir, $fileName);
            $data['original_url'] = $fileDir.$fileName;
            $data['resource_type'] = 1;
            $data = $this->saveResouceLog($data);
        } else {
            exception($file->getError(), 0, 'app\\common\\exception\\LogicException');
        }

        return $data;
    }

    public function uploadFile()
    {
        $file = request()->file('file');
        $fileDir = $this->getUploadDir('files');
        $fileName = $this->getFileName($file);
        $info = $file->move('.'.$fileDir, $fileName);
        if ($info) {
            $data['thumbnail_url'] = '';
            $data['original_url'] = $fileDir.$fileName;
            $data['resource_type'] = 2;
            $data = $this->saveResouceLog($data);
        } else {
            exception($file->getError(), 0, 'app\\common\\exception\\LogicException');
        }

        return $data;
    }

    //设置资源使用状态
    public function setResourceStatus($data)
    {
        $where[] = ['code', '=', $data['code']];
        $resourceRow = $this->curModel->where($where)->find();
        if (empty($resourceRow)) {
            exception(lang('FILE_NOT'), 0, 'app\\common\\exception\\DataEmptyException');
        } else {
            $updateData['id'] = $resourceRow['id'];
            $updateData['is_use'] = 1;

            return $this->curModel->allowField(true)->isUpdate(true)->save($updateData);
        }
    }

    private function saveResouceLog($data)
    {
        $data['status'] = 1;
        $data['is_use'] = 0;
        $postData = input();
        if (isset($postData['group']) && !empty($postData['group'])) {
            $data['group'] = $postData['group'];
        } else {
            $data['group'] = 'define';
        }
        $data['code'] = md5($data['thumbnail_url'].$data['original_url']);
        $this->curModel->allowField(true)->isUpdate(false)->save($data);

        return  $data;
    }

    //删除过期文件
    public function saveDel()
    {
        $where[] = ['create_time', '<', time() - 600];
        $where[] = ['is_use', '=', 0];
        $where[] = ['resource_type', '=', 1];
        $resourceList = $this->curModel->where($where)->all();
        if (!$resourceList->isEmpty()) {
            $ids = [];
            foreach ($resourceList as $ve) {
                if (!empty($ve['original_url']) && is_file('.'.$ve['original_url'])) {
                    unlink('.'.$ve['original_url']);
                }
                if (!empty($ve['thumbnail_url']) && is_file('.'.$ve['thumbnail_url'])) {
                    unlink('.'.$ve['thumbnail_url']);
                }
                $ids[] = $ve['id'];
            }
            $result = $this->curModel->destroy($ids);

            return $result;
        }
    }

    public function setThumbnailImg($fileDir, $fileName)
    {
        $image = \think\Image::open('.'.$fileDir.$fileName);
        $image->thumb(600, 600)->save('.'.$fileDir.'thumb_'.$fileName);

        return $fileDir.'thumb_'.$fileName;
    }

    private function getUploadDir($type = 'image')
    {
        return '/uploads/'.$type.'/'.date('Y-m-d').'/';
    }

    private function getFileName($file)
    {
        return date('H_i_s').'_'.$file->getInfo()['name'];
    }
}
